package com.yuxi.controller;

import com.yuxi.model.Student;
import com.yuxi.service.RedpackService;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import net.rubyeye.xmemcached.CASOperation;
import net.rubyeye.xmemcached.GetsResponse;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 * Created by yuxi on 16/11/6.
 */
@RestController
@RequestMapping("/del")
public class TestThreadController {
    Log log = LogFactory.getLog(TestThreadController.class);
    @Autowired
    private RedpackService redpackService;
    @Autowired
    private MemcachedClient mcc;
    final int count = 2300;


    @ApiOperation("cas")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/cas", method = RequestMethod.POST)
    public Object cas() {
//        try {
//            mcc.set("key1", 0, "value1");
//            boolean flag= true;
//            int time=0;
//            while (flag&&time<5) {
//                time++;
//                GetsResponse<Object> item = mcc.gets("key1");
//                System.out.println("GETS: value = " + item.getValue() + ",CasUnique:" + item.getCas());
////                System.out.println("SET: " + mcc.set("key1", 0, "value1_1"));
////                System.out.println("CAS: " + mcc.cas("key1", 0, "value1_1", item.getCas())); //必须FALSE
//                if (mcc.cas("key1", 0, "value1_1", item.getCas())) {
//                    flag=false;
//                    System.out.println("ok");
//                }
//            }
            return true;
//        } catch (TimeoutException e) {
//            e.printStackTrace();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (MemcachedException e) {
//            e.printStackTrace();
//        }
//        return null;
    }


    @ApiOperation("memecache")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/getMemcache", method = RequestMethod.POST)
    public Object getMemcache() {
        List<MyThread1> myThreadList = new ArrayList<>();
        for (int i = 1; i <= count; i++) {
            MyThread1 myThread1 = new MyThread1("name" + i);
            myThreadList.add(myThread1);
        }
        long start = System.currentTimeMillis();
        for (int i = 0; i < myThreadList.size(); i++) {
            new Thread(myThreadList.get(i), "name" + i).start();
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        return (end - start) + "";
    }

    @ApiOperation("发红包")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update() {
        try {
            List<MyThread> myThreadList = new ArrayList<>();
            for (int i = 1; i <= count; i++) {
                MyThread myThread = new MyThread(new Student(i, "name" + (i)));
                myThreadList.add(myThread);
            }
            long start = System.currentTimeMillis();
            for (MyThread myThread : myThreadList) {
                new Thread(myThread).start();
            }
            long end = System.currentTimeMillis();
            System.out.println(end - start);
            log.info("时间为=====" + (end - start));
            return (end - start) + "";
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    @ApiOperation("insert")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public String insert() {
        try {
            List<MyThread1> myThreadList = new ArrayList<>();
            for (int i = 1; i <= count; i++) {
                MyThread1 myThread1 = new MyThread1("");
                myThreadList.add(myThread1);
            }
            long start = System.currentTimeMillis();
            for (MyThread1 myThread : myThreadList) {
                new Thread(myThread).start();
                Thread.sleep(50);
            }
            long end = System.currentTimeMillis();
            System.out.println(end - start);
            log.info("时间为=====" + (end - start));
            return (end - start) + "";
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    class MyThread implements Runnable {
        private Student student;

        public MyThread(Student student) {
            this.student = student;
        }

        @Override
        public void run() {
            redpackService.update(student);
        }
    }

    class MyThread1 implements Runnable {
        private String name;

        public MyThread1(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            try {
                mcc.set("key1", 0, "value1");
                boolean flag= true;
                int time=0;
                while (flag&&time<2) {
                    time++;
                    GetsResponse<Object> item = mcc.gets("key1");
                    Thread.sleep(50);
                    System.out.println("GETS: value = " + item.getValue() + ",CasUnique:" + item.getCas());
                    if (mcc.cas("key1", 0, "value1_1", item.getCas())) {
                        flag=false;
                        System.out.println("GETS: value = " + item.getValue() + ",CasUnique:" + item.getCas());
                    }
                }
                if (time>2){
                    System.out.println("失败");
                }
            } catch (TimeoutException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (MemcachedException e) {
                e.printStackTrace();
            }

        }
    }
}
